﻿using System.Collections.Generic;
using System.IO;
using Lucid.ESA.ClientDataSystem.Data.Common;
using Lucid.ESA.ClientDataSystem.Data.Repositories;
using Lucid.ESA.ClientDataSystem.Domain.Entities;
using Lucid.ESA.ClientDataSystem.Domain.ParameterClasses;
using Lucid.ESA.ClientDataSystem.Domain.RepositoryContracts;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Lucid.ESA.ClientDataSystem.UnitTests.Data
{
    [TestClass]
    public class SiteRepositoryTest
    {
        [ClassInitialize()]
        public static void MyClassInitialize(TestContext testContext)
        {
            //Set the test context in the session provider, so it only creates one session
            //per test.
            SessionProvider.CurrentTestContext = testContext;
        }

        [ClassCleanup()]
        public static void MyClassCleanup() { }

        [TestInitialize()]
        public void MyTestInitialize()
        {
            //Get a fresh copy of the database.
            if (File.Exists("ClientDataSystem.sdf"))
                File.Delete("ClientDataSystem.sdf");

            File.Copy("InitialClientDataSystem.sdf", "ClientDataSystem.sdf");
        }

        [TestCleanup()]
        public void MyTestCleanup()
        {
            //Ensure session is closed after each test.
            SessionProvider.CloseSession();
        }

        [TestMethod]
        public void CanGetAllSites()
        {
            ISiteRepository siteRepository = new SiteRepository();
            IList<Site> sites = siteRepository.GetAll();
            Assert.AreEqual(4, sites.Count);
        }

        [TestMethod]
        public void CanGetSiteById()
        {
            ISiteRepository siteRepository = new SiteRepository();
            Site site = siteRepository.GetById(1L);
            Assert.AreEqual(1, site.Id.Value);
            Assert.AreEqual("6 Orange Street", site.AddressLine1);
            Assert.AreEqual("Greenville", site.AddressLine2);
            Assert.AreEqual("Whiteshire", site.AddressLine3);
            Assert.IsNull(site.AddressLine4);
            Assert.AreEqual("M1 1AA", site.PostCode);
            Assert.IsTrue(site.MainSite);
            Assert.AreEqual(1, site.Client.Id.Value);
            Assert.AreEqual("Indigo Industries", site.Client.Name);
        }

        [TestMethod]
        public void CanAddSite()
        {
            ISiteRepository siteRepository = new SiteRepository();
            IClientRepository clientRepository = new ClientRepository();
            Client client = clientRepository.LoadById(2L);

            CreateSiteParameters parameters = new CreateSiteParameters()
            {
                AddressLine1 = "17 Black Lane",
                AddressLine2 = "Tealburgh",
                AddressLine3 = "Blueshire",
                PostCode = "M1 1AA",
                MainSite = false,
                Client = client
            };

            Site site = Site.Create(parameters);

            using (ITransactionProvider transactionProvider = new TransactionProvider())
            {
                transactionProvider.BeginTransaction();
                siteRepository.Save(site);
                transactionProvider.CommitTransaction();
            }

            long newId = site.Id.Value;
            SessionProvider.CloseSession();
            Site site2 = siteRepository.GetById(newId);
            Assert.AreEqual(newId, site2.Id.Value);
            Assert.AreEqual("17 Black Lane", site2.AddressLine1);
            Assert.AreEqual("Tealburgh", site2.AddressLine2);
            Assert.AreEqual("Blueshire", site2.AddressLine3);
            Assert.IsNull(site2.AddressLine4);
            Assert.AreEqual("M1 1AA", site2.PostCode);
            Assert.IsFalse(site2.MainSite);
            Assert.AreEqual(2, site2.Client.Id.Value);
            Assert.AreEqual("Burgundy Corporation", site2.Client.Name);
        }
    }
}
